from tkinter import *
import random


root = Tk()
root.title("提问")
root.geometry("500x400")
f = open("./webpack.csv", "r+", encoding="utf-8")
data = [x.strip().split(",") for x in f.readlines()]
qst = {x for x in range(1, len(data))}

qst_id = 0  # 问题序号
data_id = 0  # 学生行ID
i = 0  # 计时器


def timer():
    """计时器"""
    global i
    i = i + 1
    timet.config(text=str(i))
    timet.after(1000, timer)


def err(info):
    """错误信息"""
    msg["text"] = info


def btn_ok():
    """登记成绩"""
    global qst_id, data_id, data, qst, score
    while True:
        try:
            score_num = int(score.get())
            if 100 >= score_num > 0:
                data[data_id][3], data[data_id][4] = qst_id, score_num
                f.seek(0)
                f.write(["\n".join([",".join([str(y) for y in x]) for x in data])][0])
                print(qst)
                btn_next()
            else:
                err("1到100")

        except Exception as e:
            print(e)
            err("请输入成绩")

        finally:
            break


def btn_next():
    """换人"""
    global qst_id, data_id, qst, data, i, stu, qt, score, msg
    # dd = [x for x in range(len(data)) if not data[x][3]]  # 没答题人的行ID
    # qst = list(
    #     set(qst) - {int(x[3]) if x[3] else None for x in data[1:]}
    # )  # 问题ID表 与已抽表 的差集
    # print("剩余 人数%s题数%s" % (len(dd), len(qst)))
    # data_id = dd[random.randint(0, len(dd))]  # 从 没答题人中随机一个 行ID
    # qst_id = qst[random.randint(0, len(qst))]  # 从 没抽问题中随机一个 问题ID
    # 重置面板信息
    stu.config(text="\n".join(data[data_id]).strip())
    qt.config(text=qst_id)
    score.delete(0, END)
    msg.config(text="")
    i = 0  # 重置计时器


# 定义信息栏
# def window_info():
# global qst_id, data_id, qst, data, i, stu, qt, score, msg

Label(root, text="学生:", font=("", 30, ""), anchor=E, width=6, height=3).grid(
    row=0, column=0, sticky=W
)
stu = Label(root, text="", font=("", 25, ""), width=14, justify="left")
stu.grid(row=0, column=1, columnspan=3, padx=0, sticky=W)

Label(root, text="题目:", font=("", 30, ""), anchor=E, width=6).grid(
    row=1, column=0, sticky=W
)
qt = Label(root, text="", font=("", 30, ""), width=14)
qt.grid(row=1, column=1, columnspan=3, sticky=W)

Label(root, text="得分:", font=("", 30, ""), anchor=E, width=6).grid(
    row=2, column=0, sticky=W
)
score = Entry(root, font=("", 30, ""), width=4)
score.grid(row=2, column=1, sticky=W)
ok = Button(root, text="OK", font=("", 30, ""), width=4, command=btn_ok)
ok.grid(row=2, column=2, padx=0, sticky=W)
next = Button(root, text="换", font=("", 30, ""), width=4, command=btn_next)
next.grid(row=2, column=3, padx=0, columnspan=2, sticky=W)
msg = Message(root, text="", font=("", 20, ""), foreground="red", width=233)
msg.grid(row=3, column=1, columnspan=2, sticky=W)

Label(root, text="计时:", font=("", 30, ""), anchor=E, width=6).grid(
    row=4, column=0, sticky=W
)
timet = Label(
    root, text="", font=("", 30, ""), bg="#fff", foreground="#f00", width=4
)
timet.grid(row=4, column=1, sticky=W)
timet.after(1000, timer)


# window_info()

root.mainloop()
f.close()  # 关闭文件
print("再见")
